{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Standalone programs and web apps with Python\n",
    "\n",
    "By [Allison Parrish](http://www.decontextualize.com)\n",
    "\n",
    "These notes are incomplete, sorry!\n",
    "\n",
    "## Standalone Python programs\n",
    "\n",
    "A Jupyter Notebook is really just a funny way of writing a Python script. A Python script is a series of Python statements collected in a plain-text file, which you can run from the command line with a Python interpreter. It's useful to know how to write standalone programs, so you can do things like write automation scripts, web apps (with frameworks like [Flask](https://flask.palletsprojects.com/en/1.1.x/)) or even video games and other multimedia applications with [Pygame](https://www.pygame.org/news). It's also just sometimes helpful to be able to run a Python program without starting up a web browser.\n",
    "\n",
    "[Notes on how to use the command line here.](http://rwet.decontextualize.com/book/unix/)\n",
    "\n",
    "* Open Terminal window\n",
    "* Explore file system with `ls`\n",
    "* Change directories with `cd`; drag folder names from the Finder\n",
    "\n",
    "Use a plain text editor like [Visual Studio Code](https://code.visualstudio.com/) or [Atom](https://atom.io/) to create a file with a `.py` extension in the same directory as your notebooks.\n",
    "\n",
    "See `scribbles.py` in this repository for a full working example.\n",
    "\n",
    "Run programs with e.g. `python scribbles.py`.\n",
    "\n",
    "* If time: [sys.argv](https://docs.python.org/3.8/library/sys.html#sys.argv)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Web applications\n",
    "\n",
    "[Introductory material on HTTP here](http://rwet.decontextualize.com/book/web-applications/).\n",
    "\n",
    "Install Flask on the command line like so:\n",
    "\n",
    "* python -m pip install Flask\n",
    "\n",
    "Follow the simple example in the above tutorial (\"A simple hello\").\n",
    "\n",
    "See `stars-server.py` in this repository for a Flask server that adapts an existing (concrete poetry) example.\n",
    "\n",
    "* If time: query strings in Flask"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Running your program on other people's computers\n",
    "\n",
    "The problem with your laptop is that only you can (easily) access it over the network, and when you close it, it doesn't listen for network connections anymore. For security reasons, this is probably for the best, but it sort of puts a damper on this whole web app thing. Why would you want a web app that only you can use?\n",
    "\n",
    "So you'll need to *deploy* your web app. \"Deployment\" is the process of putting a copy of your program on a computer that *is* consistently connected to the Internet and has a fixed IP address and/or hostname. This almost always involves paying to use someone else's computer—usually a VPS (\"virtual private server\") run by a third party.\n",
    "\n",
    "Some places to deploy your Python web apps:\n",
    "\n",
    "* [Amazon web services](https://towardsdatascience.com/deploying-a-python-web-app-on-aws-57ed772b2319) (pro: powerful, flexible, can be cheap; con: awful and immoral corporation, very barebones, confusing and user-hostile admin interface); similar offerings from [Google](https://cloud.google.com/) and [Microsoft](https://azure.microsoft.com/en-us/)\n",
    "* [Heroku](https://www.heroku.com/python) (pro: easy to use for straightforward use cases; con: sorta off in its own world, can be pricey)\n",
    "* [Digital Ocean](https://www.digitalocean.com/community/tutorials/how-to-build-and-deploy-a-flask-application-using-docker-on-ubuntu-18-04) (pro: inexpensive and uncomplicated; con: not as flexible as some other options, requires a good working knowledge of command line stuff)\n",
    "* [Python Anywhere](https://www.pythonanywhere.com/pricing/) (pro: I dunno, I just like these guys; con: free offering is very restricted)\n",
    "\n",
    "### Python on Glitch\n",
    "\n",
    "For this tutorial, we'll use [Glitch](https://glitch.com/), which has a user interface that is very friendly for beginners and is free (with a [paid option for more powerful features](https://glitch.com/pricing)).\n",
    "\n",
    "Instead of editing the code on your computer with a plain text editor, you can edit the code right there through Glitch's web interface. (You can also import/export from GitHub. Each Glitch project also has a Git URL that you can push to. So you can also edit locally and upload to Glitch to run it there if you want.)\n",
    "\n",
    "Glitch is intended for Node developers and it only unofficially supports Python. But it's super usable if you get everything set up right. Fortunately for you, I've made a [template with all the basics](https://glitch.com/~minimal-python3-flask) (click on \"View Source\" and then \"Remix.\")\n",
    "\n",
    "One big disadvantage of Glitch right off the bat is that each project has a *200mb limit*. This means that you can *just* barely install Flat and Bezmerizing along with their dependencies. Not a lot of room is left over for other media files, and installing libraries like Tensorflow is right out. (Let me know if you want to strategize about getting around this.)\n",
    "\n",
    "Explanation of the files in the project:\n",
    "\n",
    "* `glitch.json`: A Glitch-specific configuration file. I don't really know what all of this stuff does, and it's not documented anywhere as far as I know. But it does instruct Glitch to run particular commands when it starts up your server.\n",
    "* `requirements.txt`: A list of Python modules to install. This is the thing you'd put after `pip install`. One module per line. [More on Pip requirements files](https://pip.pypa.io/en/stable/user_guide/#requirements-files).\n",
    "* `server.py`: Your Flask application.\n",
    "\n",
    "Two other examples on Glitch:\n",
    "\n",
    "* [Random stars](https://glitch.com/~random-stars-python3)\n",
    "* [Scribbles](https://glitch.com/~mol-asemic-svg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
